package com.can.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.can.domain.ResponseResult;
import com.can.domain.entity.Menu;
import com.can.domain.vo.MenuTreeVO;
import com.can.service.MenuService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult list(String status, String menuName) {
        List<Menu> list = menuService.list(new LambdaQueryWrapper<Menu>()
                .eq(StringUtils.hasText(status), Menu::getStatus, status)
                .like(StringUtils.hasText(menuName), Menu::getMenuName, menuName)
                .orderByAsc(Menu::getOrderNum)
        );
        return ResponseResult.okResult(list);
    }

    @PostMapping
    public ResponseResult add(@RequestBody Menu menu) {
        menuService.save(menu);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult get(@PathVariable Long id) {
        return ResponseResult.okResult(menuService.getById(id));
    }

    @PutMapping
    public ResponseResult update(@RequestBody @Valid Menu menu) {
        menuService.update(menu);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{menuId}")
    public ResponseResult delete(@PathVariable Long menuId) {
        menuService.delete(menuId);
        return ResponseResult.okResult();
    }

    @GetMapping("/treeselect")
    public ResponseResult treeselect() {
        return ResponseResult.okResult(menuService.selectRouterMenuTreeAll());
    }

    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult<MenuTreeVO> roleMenuTreeselect(@PathVariable Long id) {
        return ResponseResult.okResult(menuService.selectRouterMenuTreeByRoleId(id));
    }
}